package com.ruoyi.system.initial;

import com.ruoyi.system.domain.StationMap;
import com.ruoyi.system.service.IStationMapService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JerryLu
 * @date 2024/8/6 18:37
 * @description 收费站信息加载线程
 */
@Component
public class StationMapThread {
    private final Logger logger = LoggerFactory.getLogger(StationMapThread.class);

    private final String KEY_ALL = "ALL";

    private Map<String, String> stationMap = new HashMap<>();

    @Autowired
    private IStationMapService mapService;

    // 在构造函数中初始化pattern,只编译一次
    public StationMapThread() {
    }

    /**
     * 初始化更新各类数据信息
     *
     * @return void
     * @author JerryLu
     * @date 2024/8/15 13:04
     */
    public void execute() {
        Map<String, String> map = loadStationDtoMap();
        if (null == map || map.isEmpty()) {
            throw new RuntimeException("收费站信息加载失败");
        }
        stationMap = map;

    }

    /**
     * 根据userId获取对应坐席人员库
     *
     * @param stationHex
     * @return
     */
    public String getStationInfo(String stationHex) {
        String userInfo = stationMap.get(stationHex);

        if (null != userInfo) {
            return userInfo;
        }
        return stationMap.get(KEY_ALL);
    }

    private Map<String, String> loadStationDtoMap() {
        try {
            Map<String, String> map = new HashMap<>();
            StationMap stationMap1 = new StationMap();
            List<StationMap> list = mapService.selectStationMapList(stationMap1);
            for (StationMap user : list) {
                map.put(user.getStationHex(), user.getStationName());
            }
            return map;
        } catch (Throwable t) {
            logger.error(String.valueOf(t), t);
        }
        return null;
    }
}
